package org.aplikator.client.local.command;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceHistoryMapper;

public class AplikatorActivityMapper implements ActivityMapper, PlaceHistoryMapper {

    public AplikatorActivityMapper() {
        super();
    }

    @Override
    public Activity getActivity(Place place) {
        if (place instanceof AplikatorActivity.Place)
            return ((AplikatorActivity.Place) place).getActivity();
        return null;
    }

    @Override
    public Place getPlace(String token) {
        int colonAt = token.indexOf(':');
        String initial;
        String rest;
        if (colonAt >= 0) {
            initial = token.substring(0, colonAt);
            rest = token.substring(colonAt + 1);
            if (ListEntities.class.getName().equals(initial)){
                return new ListEntities(rest).getPlace();
            }else if (ExecuteFunction.class.getName().equals(initial)){
                return new ExecuteFunction(rest).getPlace();
            }
        }
        return null;
    }

    @Override
    public String getToken(Place place) {
        if (place instanceof AplikatorActivity.Place) {
            return ((AplikatorActivity.Place) place).getToken();
        }
        return null;
    }
}